//
//  TestReversiBoardLineRange.m
//  Reversi
//
//  Created by 成瀬 龍人 on 2/6/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TestReversiBoardLineRange.h"
#import "ReversiBoardLineRange.h"
#import "ReversiBoardLineDirection.h"
#import "ReversiBoardPosition.h"

@implementation TestReversiBoardLineRange
- (void)setUp{
}

- (void)tearDown{
}

- (void)testCreateLineRange{
	@try{
		ReversiBoardPosition* pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
		ReversiBoardLineDirection* dir = [[ReversiBoardLineDirection alloc] init:0:1];
		int num = 5;
		ReversiBoardLineRange* line = [[ReversiBoardLineRange alloc] init:pos
																	  dir:dir
																	  num:num];
		int i = 0;
		for(ReversiBoardPosition* _pos in line){
			STAssertEquals(_pos.x, pos.x + dir.x * i, @"x is %d but expect %d",_pos.x,_pos.x + dir.x * i);
			STAssertEquals(_pos.y, pos.y + dir.y * i, @"y is %d but expect %d",_pos.y,_pos.y + dir.y * i);
			STAssertTrue(i < num, @"position num");
			++i;
		}
		pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
		dir = [[ReversiBoardLineDirection alloc] init:0:-1];
		num = 4;
		line = [[ReversiBoardLineRange alloc] init:pos
											   dir:dir
											   num:num];
		i = 0;
		for(ReversiBoardPosition* _pos in line){
			STAssertEquals(_pos.x, pos.x + dir.x * i, @"x is %d but expect %d",_pos.x,_pos.x + dir.x * i);
			STAssertEquals(_pos.y, pos.y + dir.y * i, @"y is %d but expect %d",_pos.y,_pos.y + dir.y * i);
			STAssertTrue(i < num, @"position num");
			++i;
		}
		pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
		dir = [[ReversiBoardLineDirection alloc] init:1:0];
		num = 5;
		line = [[ReversiBoardLineRange alloc] init:pos
											   dir:dir
											   num:num];
		i = 0;
		for(ReversiBoardPosition* _pos in line){
			STAssertEquals(_pos.x, pos.x + dir.x * i, @"x is %d but expect %d",_pos.x,_pos.x + dir.x * i);
			STAssertEquals(_pos.y, pos.y + dir.y * i, @"y is %d but expect %d",_pos.y,_pos.y + dir.y * i);
			STAssertTrue(i < num, @"position num");
			++i;
		}
		pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
		dir = [[ReversiBoardLineDirection alloc] init:-1:0];
		num = 4;
		line = [[ReversiBoardLineRange alloc] init:pos
											   dir:dir
											   num:num];
		i = 0;
		for(ReversiBoardPosition* _pos in line){
			STAssertEquals(_pos.x, pos.x + dir.x * i, @"x is %d but expect %d",_pos.x,_pos.x + dir.x * i);
			STAssertEquals(_pos.y, pos.y + dir.y * i, @"y is %d but expect %d",_pos.y,_pos.y + dir.y * i);
			STAssertTrue(i < num, @"position num");
			++i;
		}
	}@catch(NSException* e){
		STFail(@"exception %@", e.description);
	}
}

- (void)testErrorCase{
	ReversiBoardPosition* pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
	ReversiBoardLineDirection* dir = [[ReversiBoardLineDirection alloc] init:1:1];
	int num = 6;
	STAssertThrows([[ReversiBoardLineRange alloc] init:pos
												   dir:dir
												   num:num], @"out of range");
	pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
	dir = [[ReversiBoardLineDirection alloc] init:1:-1];
	num = 5;
	STAssertThrows([[ReversiBoardLineRange alloc] init:pos
												   dir:dir
												   num:num], @"out of range");
	pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
	dir = [[ReversiBoardLineDirection alloc] init:-1:-1];
	num = 5;
	STAssertThrows([[ReversiBoardLineRange alloc] init:pos
												   dir:dir
												   num:num], @"out of range");
	pos = [[ReversiBoardPosition alloc] initWithInt:4:4];
	dir = [[ReversiBoardLineDirection alloc] init:-1:1];
	num = 5;
	STAssertThrows([[ReversiBoardLineRange alloc] init:pos
												   dir:dir
												   num:num], @"out of range");
}
@end
